package com.tbabs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbabs.common.R;
import com.tbabs.dto.SetmealDto;
import com.tbabs.entity.Category;
import com.tbabs.entity.Setmeal;
import com.tbabs.service.CategoryService;
import com.tbabs.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功！");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> getSetmealByPage(int page, int pageSize, @RequestParam(required = false) String name){
        Page<Setmeal> setmealInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealInfo, queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(setmealInfo, setmealDtoPage, "records");

        List<Setmeal> setmealList = setmealInfo.getRecords();
        List<SetmealDto> setmealDtoList = setmealList.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Category category = categoryService.getById(item.getCategoryId());

            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }

            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除套餐成功！");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.set(Setmeal::getStatus, status)
                .in(Setmeal::getId, ids);
        setmealService.update(lambdaQueryWrapper);
        return R.success("更新状态成功！");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealInfo(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getInfoByIDWithDish(id);

        return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改信息成功！");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "'setmeal_' + #setmeal.categoryId")
    public R<List<Setmeal>> getSetmealList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(setmeal.getStatus() != null, Setmeal::getStatus, 1)
                .orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
